	YAHOO.widget.DataTable.customLocFormatter = function(elLiner, oRecord, oColumn, oData) {
		var str = "";
		if(!oData || oData == null || oData.length <= 0){
			str = "No other locations";
		}else{
			str = oData.length + " other location(s)";
		}
	    elLiner.innerHTML = str;
	};

function open_mng_supp(){
	u_hideAllSections();
	u_safeShowElement('supp_mgmt_div');
	getPreferredSuplierList();
	document.getElementById('searchResults_supp_div').style.display = 'none';
	//document.getElementById('supplier_search_div').style.display = 'none';
	//document.getElementById('registered_suppliers_div').style.display = '';
	//var tabView = new YAHOO.widget.TabView('manageSupplierTab'); 
	domtab.showTabByName("aAddSupplier");
	dwr.util.setValue('supp_actionplaceholder','');
	
	if(!isNavigationFromBackButton){
	var functionPointerObj = new Object();
	functionPointerObj.functionName = "open_mng_supp()";
	navigationStateArray.push(functionPointerObj);
	}
}

function tn_manageSupplier_serachSupplier(searchObj){
	searchObj = "";
	var config = {
			callback : success_serachSupplier,
			exceptionHandler : exception_generic_handler,
			timeout : 100000,
			errorHandler : exception_generic_handler	
	};
	searchObj = $("supp_cname").value;
	DWR_Register.searchCompany(searchObj,config);
}

function success_serachSupplier(companyList){
	/*var records = new Array();
	 for(i = 0;i<companyList.objRespOrError.length;i++){
		 var record = companyList.objRespOrError[i];
		 var record1 = {};
		 record1.companyName = record.companyName;
		 if(record.paymentTerms != null && record.paymentTerms.length > 0){
			 record1.paymentTerms = "<a href='javascript:void(0);' onclick='javascript:void(0);'>Click to see Payment Terms.</a>"
		 }else{
			 record1.paymentTerms = "<a href='javascript:void(0);' onclick='javascript:void(0);'>Click to see Payment Terms.</a>"
		 }
		 record1.city = record.city;
		 if(record.locations != null && record.locations.length > 0){
			 record1.locations = "<a href='javascript:void(0);' onclick='javascript:void(0);'>Click to see Locations.</a>";
		 }else{
			 record1.locations = "<a href='javascript:void(0);' onclick='javascript:void(0);'>Click to see Locations.</a>";
		 }
		 record1.companyId = record.companyId;
		 
		 if(usrcompany_id != record.companyId){
			 records.push(record1);
		 }
	 }*/
	var companyList2 = new Array();
	for(var i=0;i<companyList.objRespOrError.length;i++){
		if(usrcompany_id != companyList.objRespOrError[i].companyId){
			companyList2.push(companyList.objRespOrError[i]);
		 }
	}
	create_manageSuuplierSerachTable(companyList2);
	document.getElementById('searchResults_supp_div').style.display = '';
}

var manageSupplierColumnDefs = [ 

{key:"check", label:"Select",
formatter:"checkbox"},

{
	key : "companyName",
	label : "Supplier Name",
	width : "40px"
	
},
{
	key : "companyId",
	width : "0px",
	hidden: true
	
},

{
	key : "address",
	label : "Address",
	width : "40px"
	
},

{
	key : "city",
	label : "City",
	width : "40px"
	
},
{
	key : "state",
	label : "State",
	width : "40px"
	
},

{
	key : "zip",
	label : "Zip",
	width : "40px"
	
},

{
	key : "country",
	label : "Country",
	width : "40px"
	
},

{
	key : "contact",
	label : "Contact",
	width : "40px"
	
},

{
	key : "locations",
	label : "All Locations",
	formatter: YAHOO.widget.DataTable.customLocFormatter,
	width : "40px"
	
}
];


var registeredSupplierColumnDefs = [ 

{key:"check", label:"Select",
formatter:"checkbox"},

{
	key : "companyName",
	label : "Company Name",
	width : "40px"
	
},
{
	key : "companyId",
	width : "0px",
	hidden: true
	
},

{
	key : "paymentTerms",
	label : "Payment Terms",
	width : "40px"
	
},

{
	key : "city",
	label : "City",
	width : "40px"
	
},

{
	key : "locations",
	label : "All Locations",
	width : "40px",
	formatter: YAHOO.widget.DataTable.customLocFormatter
	
}
];

var manageSupplierSerach_yui_table_instance = null;
var registeredSupplier_yui_table_instance = null;
function create_manageSuuplierSerachTable(records) {
	
	if(manageSupplierSerach_yui_table_instance == null){
		var manageSupplierDataSource = new YAHOO.util.DataSource();
		manageSupplierDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
		
		var oConfigs = {
	             paginator: new YAHOO.widget.Paginator({
	                 rowsPerPage: 5
	                 
	             }),
	             initialRequest: "results=504",
	             width: "600px",
	             // we'll leave the actual loading for later
	 			initialLoad:true
	     };
		
		manageSupplierSerach_yui_table_instance = new YAHOO.widget.DataTable(
				"searchSupplierYUITable",
				manageSupplierColumnDefs,
				manageSupplierDataSource,
				oConfigs);
		manageSupplierSerach_yui_table_instance.set("selectionMode", "single");
		manageSupplierSerach_yui_table_instance.subscribe("rowClickEvent",
				manageSupplierSerach_yui_table_instance.onEventSelectRow);
		manageSupplierSerach_yui_table_instance.subscribe("checkboxClickEvent", function(oArgs){   
	    	var elCheckbox = oArgs.target;
			var newValue = elCheckbox.checked;
			var record = this.getRecord(elCheckbox);
			var column = this.getColumn(elCheckbox);
			record.setData(column.key,newValue);
	     }); 
	}
	manageSupplierSerach_yui_table_instance.deleteRows(0,manageSupplierSerach_yui_table_instance.getRecordSet().getRecords().length);
	manageSupplierSerach_yui_table_instance.addRows(records);
	manageSupplierSerach_yui_table_instance.render();

}

function create_registeredSuuplierSerachTable(records) {
	if(registeredSupplier_yui_table_instance == null){
		var registeredSupplierDataSource = new YAHOO.util.DataSource();
		registeredSupplierDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
		registeredSupplier_yui_table_instance = new YAHOO.widget.DataTable(
				"registeredSupplierYUITable",
				registeredSupplierColumnDefs,
				registeredSupplierDataSource,
				{});
		registeredSupplier_yui_table_instance.set("selectionMode", "single");
		registeredSupplier_yui_table_instance.subscribe("rowClickEvent",
				registeredSupplier_yui_table_instance.onEventSelectRow);
		registeredSupplier_yui_table_instance.subscribe("checkboxClickEvent", function(oArgs){   
	    	var elCheckbox = oArgs.target;
			var newValue = elCheckbox.checked;
			var record = this.getRecord(elCheckbox);
			var column = this.getColumn(elCheckbox);
			record.setData(column.key,newValue);
	     }); 
	}
	registeredSupplier_yui_table_instance.deleteRows(0,registeredSupplier_yui_table_instance.getRecordSet().getRecords().length);
	registeredSupplier_yui_table_instance.addRows(records);
	registeredSupplier_yui_table_instance.render();

}

function getSelectedSupplier(){
	if(manageSupplierSerach_yui_table_instance.getState().selectedRows == null || manageSupplierSerach_yui_table_instance.getState().selectedRows <= 0){
		alert("No Supplier selected.");
		return;
	}
	var recordset = manageSupplierSerach_yui_table_instance.getRecordSet().getRecords();
	
	var supplierList = new Array();
	for(i=0;i<recordset.length;i++){
		var record = recordset[i];
		if(record.getData('check')){
			supplierList.push(record.getData('companyId'));
		}
	}
	
	
	//Get the already existing ones:
	recordset = null;
	recordset = registeredSupplier_yui_table_instance.getRecordSet().getRecords();
	for(j=0;j<recordset.length;j++){
		var record = recordset[j];
		supplierList.push(record.getData('companyId'));
	}
	
	addPreferredSuplierList(usrcompany_id,supplierList,"ADD");
}

function addPreferredSuplierList(usrcompany_id,supplierList,action){
	
	var config = {
			callback : success_addPreferredSuplierList,
			exceptionHandler : exception_generic_handler,
			timeout : 100000,
			errorHandler : exception_generic_handler	
	};
	DWR_Register.addRemovePreferredSuplierList(usrcompany_id,supplierList,action,config);
}

function success_addPreferredSuplierList(mySuppliers){
	
	var records = new Array();
	 for(i = 0;i<mySuppliers.objRespOrError.length;i++){
		 var record = mySuppliers.objRespOrError[i];
		 var record1 = {};
		 record1.companyName = record.companyName;
		 record1.paymentTerms = record.paymentTerms;
		 record1.city = record.city;
		 record1.locations = record.locations;
		 record1.companyId = record.companyId;
		 records.push(record1);
	 }
	create_registeredSuuplierSerachTable(records);
	alert("Supplier Managed Successfully!!");
	
	dwr.util.setValue('supp_actionplaceholder','Supplier Information updated successfully. Showing your registered suppliers.');
	//document.getElementById('supplier_search_div').style.display = 'none';
	//document.getElementById('registered_suppliers_div').style.display = '';
}

function deletePreferredSupplier(){
	if(registeredSupplier_yui_table_instance.getState().selectedRows == null || registeredSupplier_yui_table_instance.getState().selectedRows <= 0){
		alert("No Supplier selected.");
		return;
	}
	var recordset = registeredSupplier_yui_table_instance.getRecordSet().getRecords();
	var supplierList = new Array();
	for(i=0;i<recordset.length;i++){
		var record = recordset[i];
		if(record.getData('check')){
			supplierList.push(record.getData('companyId')); //new array list will be updated in back end.
		}
	}
	addPreferredSuplierList(usrcompany_id,supplierList,"DELETE");
	
}

function getPreferredSuplierList(){
	var config = {
			callback : success_getPreferredSuplierList,
			exceptionHandler : exception_generic_handler,
			timeout : 100000,
			errorHandler : exception_generic_handler	
	};
	DWR_Register.getPreferredSuplierList(usrcompany_id,config);
}

function success_getPreferredSuplierList(mySuppliers){
	var records = new Array();
	 for(i = 0;i<mySuppliers.objRespOrError.length;i++){
		 var record = mySuppliers.objRespOrError[i];
		 if(record.companyId != usrcompany_id){
			 var record1 = {};
			 record1.companyName = record.companyName;
			 record1.paymentTerms = record.paymentTerms;
			 record1.city = record.city;
			 record1.locations = record.locations;
			 record1.companyId = record.companyId;
			 records.push(record1);
		 }
	 }
	create_registeredSuuplierSerachTable(records);
}


function getClientsList(suppIds){
	var config = {
			callback : success_getClientList,
			exceptionHandler : exception_generic_handler,
			timeout : 100000,
			errorHandler : exception_generic_handler	
	};
	DWR_Register.getClientsList(suppIds,config);
}

var supp_clientCache = null;

function success_getClientList(list){
	//Create acted and non-acted lists.
	var actedlist = new Array();
	var nonactedlist = new Array();
	
	if(list.objRespOrError && list.objRespOrError != null && list.objRespOrError.length > 0){
		supp_clientCache = new Array();
		supp_clientCache.push({companyUniqueIdentifier: null, companyName: "  "});
		for(var i=0;i<list.objRespOrError.length;i++){
			supp_clientCache.push(list.objRespOrError[i]);
			if(list.objRespOrError[i].assoStatus == "NEW"){
				nonactedlist.push(list.objRespOrError[i]);
			}else{
				actedlist.push(list.objRespOrError[i]);
			}
		}
		
		
		dwr.util.removeAllOptions("cmo_client");
		dwr.util.addOptions("cmo_client",supp_clientCache,"companyUniqueIdentifier","companyName");
		dwr.util.removeAllOptions("inv_client_sel");
		dwr.util.addOptions("inv_client_sel",supp_clientCache,"companyUniqueIdentifier","companyName");
	
	}
	create_manageClientsTable(nonactedlist);
	create_manageClientsTable2(actedlist);
}

var clientListByCompanyYUITable = null;
var clientListByCompanyYUITable2 = null;
//create_manageClientsTable(list)
function create_manageClientsTable(list) {
	if(clientListByCompanyYUITable == null){
		YAHOO.widget.DataTable.customAddrFormatter = function(elLiner, oRecord, oColumn, oData) {
		    elLiner.innerHTML = oRecord._oData.addressLine1; + " " + oRecord._oData.addressLine2 + " " + oRecord._oData.city + " " + oRecord._oData.state + " " + oRecord._oData.country;
		};
		
		YAHOO.widget.DataTable.customAssoFormatter = function(elLiner, oRecord, oColumn, oData) {
			var idsuf = oRecord.getId();
			var namerad = "rd_actnclnt" + idsuf;
			var idpref = namerad + "_id_";
			var idaccept = idpref + "accept";
			var idreject = idpref + "reject";
			var idpending = idpref + "pending";
			
			
			var inhtma = "<input type='radio' name='" + namerad + "' id='" +idaccept+"'><strong>Accept</strong></input>";
			var inhtmr = "<input type='radio' name='" + namerad + "' id='" +idreject+"'><strong>Reject</strong></input>";
			var inhtmp = "<input type='radio' name='" + namerad + "' id='" +idpending+"'><strong>Pending</strong></input>";
		    elLiner.innerHTML = inhtma + "&nbsp;&nbsp;&nbsp;" + inhtmr + "&nbsp;&nbsp;&nbsp;" + inhtmp + "&nbsp;";
		};

		
		var clientColumnDefs = 
			[{ key : "companyName", label : "Company Name", width : "40px"},
			 { key : "companyId", width : "0px", hidden: true }, 
			 { key : "address", label : "Address", width : "40px", formatter: YAHOO.widget.DataTable.customAddrFormatter},
			 { key : "phone", label : "Phone", width : "40px"},
			 { key : "contact", label : "Contact", width : "40px"},
			 { key : "assoStatus", label : "", width : "70px", formatter: YAHOO.widget.DataTable.customAssoFormatter}
		];
		
		var clientDataSource = new YAHOO.util.DataSource();
		clientDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
		clientListByCompanyYUITable = new YAHOO.widget.DataTable(
				"clientListByCompany_YUI_Table_Div",
				clientColumnDefs,
				clientDataSource,
				{});
	}
	
	clientListByCompanyYUITable.deleteRows(0,clientListByCompanyYUITable.getRecordSet().getRecords().length);
	clientListByCompanyYUITable.addRows(list);
	clientListByCompanyYUITable.render();
}

function create_manageClientsTable2(list) {
	if(clientListByCompanyYUITable2 == null){
		var clientColumnDefs = 
			[{ key : "companyName", label : "Company Name", width : "40px"},
			 { key : "companyId", width : "0px", hidden: true }, 
			 { key : "address", label : "Address", width : "40px", formatter: YAHOO.widget.DataTable.customAddrFormatter},
			 { key : "phone", label : "Phone", width : "40px"},
			 { key : "contact", label : "Contact", width : "40px"},
			 { key : "lastUpdateDate", label : "Accepted Date", width : "40px", formatter: "date"}
		];
		
		var clientDataSource = new YAHOO.util.DataSource();
		clientDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
		clientListByCompanyYUITable2 = new YAHOO.widget.DataTable(
				"clientListByCompany_YUI_Table_Div2",
				clientColumnDefs,
				clientDataSource,
				{});
	}
	
	clientListByCompanyYUITable2.deleteRows(0,clientListByCompanyYUITable2.getRecordSet().getRecords().length);
	clientListByCompanyYUITable2.addRows(list);
	clientListByCompanyYUITable2.render();
}

function updClientStatus(){
	var updatedata = new Array();
	var requireupdate = false;
	if(clientListByCompanyYUITable.getRecordSet() != null && clientListByCompanyYUITable.getRecordSet().getLength() > 0){
		for(var i=0;i<clientListByCompanyYUITable.getRecordSet().getLength();i++){
			var rec = clientListByCompanyYUITable.getRecordSet().getRecord(i);
			var aid = "rd_actnclnt"+rec.getId()+"_id_accept";
			var rid = "rd_actnclnt"+rec.getId()+"_id_reject";
			var pid = "rd_actnclnt"+rec.getId()+"_id_pending";
			var data = {supplierId: usr_comp_cache.companyId, clientId: rec.getData("companyId") , assoStatus: ""};
			if(document.getElementById(aid) && document.getElementById(aid).checked){
				data.assoStatus = "ACCEPTED";
				requireupdate = true;
			}else if(document.getElementById(rid) && document.getElementById(rid).checked){
				data.assoStatus = "REJECTED";
				requireupdate = true;
			}else if(document.getElementById(pid) && document.getElementById(pid).checked){
				data.assoStatus = "PENDING";
				requireupdate = true;
			} 
			
			if(requireupdate){
				updatedata.push(data);
			}
		}
		
	}
	if(requireupdate){
		var config = {
				callback : success_getClientList,
				exceptionHandler : exception_generic_handler,
				timeout : 100000,
				errorHandler : exception_generic_handler	
		};
		
		DWR_Register.updateAssociation(updatedata,config);
	}
	
	
}